package com.avit.tv.kui.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author like
 * @date 2020/7/16
 */
public class TimeUtils {

    private final static String secFormat = "HH:mm:ss";

    private final static String minFormat = "HH:mm";

    private final static String normalFormat = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取时间换算后的long 值用来确定最大进度
     *
     * @param time
     * @param timeFormat
     * @return
     */
    public static long getLongTimeSec(String time, String timeFormat) {
        try {
            @SuppressLint("SimpleDateFormat")
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeFormat);
            Date parseTime = simpleDateFormat.parse(time);
            long timeLong = parseTime.getTime() / 1000;
            return timeLong;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取时间格式化后的值
     *
     * @param millis     毫秒数
     * @param timeFormat
     * @return
     */
    public static String parseLongTime(long millis, String timeFormat) {
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeFormat);
        Date preFormat = new Date(millis);
        String format = simpleDateFormat.format(preFormat);
        if (TextUtils.isEmpty(format)) return "";
        else return format;
    }


    /**
     * 获取 sec format
     *
     * @param time 单位秒
     * @return
     */
    public static String getTimeSec(long time) {
        return parseLongTime(time * 1000, secFormat);
    }

    /**
     * 获取 min format
     *
     * @param time
     * @return
     */
    public static String getTimeMin(long time) {
        return parseLongTime(time * 1000, minFormat);
    }


    /**
     * 获取标准时间制式 format 的秒数
     *
     * @param time
     * @return
     */
    public static long getLongTimeNormal(String time) {
        return getLongTimeSec(time, normalFormat);
    }


    /**
     * 解析 long 时间换算为 标准时间制式
     *
     * @param sec 【单位： 秒】
     * @return
     */
    public static String parseLongTimeNormal(long sec) {
        return parseLongTime(sec * 1000L, normalFormat);
    }


    public static final class TimeValued{
        public final static int ZERO = 0;
        public final static int FIFTEEN_MIN = 15;
        public final static int THIRTY_MIN = 30;
        public final static int FORTY_FIVE_MIN = 45;
        public final static int ONE_HOUR_MIN = 60;
        public final static int ONE_MIN_SEC = 60;

        public static int[] getValueMins(){
            return new int[]{ZERO,FIFTEEN_MIN,THIRTY_MIN,FORTY_FIVE_MIN};
        }
    }
}
